/*
 * Name StateDao.java
 * Author ZhangZhenli
 * Created on 2012-12-5, 下午8:07:06
 * 
 * Copyright (c) 2012 NanJing YiWuXian Network Technology Co., Ltd. All rights
 * reserved
 */
package com.house365.app.analyse.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.house365.app.analyse.data.AnalyseMetaData;

/**
 * 
 * @author ZhangZhenli
 */
public class AnalyticsDao extends AnalyticsDB {
    /**
     * @param context
     */
    public AnalyticsDao(Context context) {
        super(context);
    }

    public List<AnalyseMetaData> select() {
        ArrayList<AnalyseMetaData> list = new ArrayList<AnalyseMetaData>();;
        Cursor cursor = null;
        try {
            cursor = db.rawQuery("SELECT "
                                            +"analytics.uuid, "
                                            +"analytics.app, "
                                            +"analytics.version, "
                                            +"analytics.platform, "
                                            +"analytics.deviceid, "
                                            +"analytics.event, "
                                            +"analytics.eventobj, "
                                            +"analytics.ip, "
                                            +"analytics.location, "
                                            +"analytics.optime, "
                                            +"analytics.page, "
                                            +"analytics.provider, "
                                            +"analytics.screen, "
                                            +"analytics.model, "
                                            +"analytics.starttime, "
                                            +"analytics.endtime, "
                                            +"analytics.channel, "
                                            +"analytics.network, "
                                            +"analytics.city, "
                                            +"analytics.os_version "
                                          +"FROM \"analytics\"", null);
            while(cursor.moveToNext()){
                AnalyseMetaData data = new AnalyseMetaData();
                 data.setUuid(cursor.getString(0));
                 data.setApp(cursor.getString(1));
                 data.setVersion(cursor.getString(2));
                 data.setPlatform(cursor.getInt(3));
                 data.setDeviceid(cursor.getString(4));
                 data.setEvent(cursor.getInt(5));
                 data.setEventobj(cursor.getString(6));
                 data.setIp(cursor.getString(7));
                 data.setLocation(cursor.getString(8));
                 data.setOptime(cursor.getInt(9));
                 data.setPage(cursor.getString(10));
                 data.setProvider(cursor.getString(11));
                 data.setScreen(cursor.getString(12));
                 data.setModel(cursor.getString(13));
                 data.setStarttime(cursor.getInt(14));
                 data.setEndtime(cursor.getInt(15));
                 data.setChannel(cursor.getString(16));
                 data.setNetwork(cursor.getString(17));
                 data.setCity(cursor.getString(18));
                 data.setOs_version(cursor.getString(19));
                list.add(data);
            }
        } finally {
            closeQuietly(cursor);
        }
        return list;
    }
    
    public int conut() {
        int connt = 0;
        Cursor cursor = null;
        try {
            cursor = db.rawQuery("SELECT count(*) FROM \"main\".\"analytics\"", null);
            cursor.moveToFirst();
            connt = cursor.getInt(0);
        } finally {
            closeQuietly(cursor);
        }
        return connt;
    }

    /**
     * 插入数据
     * @param data
     */
    public void insert(AnalyseMetaData data){
        db.execSQL("INSERT INTO \"main\".\"analytics\" "
                + "(\"uuid\","
                + " \"app\","
                + " \"version\","
                + " \"platform\","
                + " \"deviceid\","
                + " \"event\","
                + " \"eventobj\","
                + " \"ip\","
                + " \"location\","
                + " \"optime\","
                + " \"page\","
                + " \"provider\","
                + " \"screen\","
                + " \"model\", "
                + " \"starttime\","
                + " \"endtime\", "
                + " \"channel\", "
                + " \"network\","
                + " \"city\","
                + " \"os_version\") "
                + " VALUES "
                + " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 
                new Object[] {
                        data.getUuid(),
                        data.getApp(),
                        data.getVersion(),
                        data.getPlatform(),
                        data.getDeviceid(),
                        data.getEvent(),
                        data.getEventobj(),
                        data.getIp(),
                        data.getLocation(),
                        data.getOptime(),
                        data.getPage(),
                        data.getProvider(),
                        data.getScreen(),
                        data.getModel(),
                        data.getStarttime(),
                        data.getEndtime(),
                        data.getChannel(),
                        data.getNetwork(),
                        data.getCity(),
                        data.getOs_version()
                        });
    }

    public void update(AnalyseMetaData data) {       
//         final SQLiteDatabase db = mSQLiteHelper.getWritableDatabase();
//         db.beginTransaction();
//         try {
//         // db.execSQL("UPDATE \"main\".\"analytics\"", new Object[] { });
//         db.setTransactionSuccessful();
//         } finally {
//         db.endTransaction();
//         }
        throw new UnsupportedOperationException();

    }

    /**
     * 清空数据库表
     */
    public void delete(AnalyseMetaData data) {
        db.execSQL("DELETE  FROM analytics WHERE analytics.uuid = ?", new Object[] { data.getUuid() });
    }
    /**
     * 清空数据库表
     */
    public void clear() {
        db.execSQL("DELETE FROM \"analytics\"");
    }

    public void closeQuietly(Cursor c) {
        try {
            if (c != null) {
                c.close();
            }
        } catch (Exception e) {
        }
    }
}
